<template>
  <div class="album">
    <div class="albuminfo" v-for="item in albums" :key="item.id" @click="toAlbumPage(item.id)">
      <div class="left">
        <img :src="item.picUrl" />
        <span class="name">{{ item.name }}</span>
      </div>
      <div class="artist">
        <span>{{item.artist.name }}</span>
        <span> ({{ item.artist.alias[0] }})</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCloudSearch } from '../../network/search.js'
export default {
  props: ['type', 'value'],
  data () {
    return {
      albums: [],
      albumCount: 0
    }
  },
  methods: {
    toAlbumPage (id) {
      this.$router.push('/album_detail/' + id)
    },
    getCloudSearch (type, keywords) {
      getCloudSearch(type, keywords).then(res => {
        // console.log(res.data)
        this.albums = res.data.result.albums
        this.albumCount = res.data.result.albumCount
        this.$emit('singerCount', this.albumCount + ' 首专辑')
        // console.log(this.albums)
      })
    }
  },
  created () {
    this.getCloudSearch(this.type, this.value)
  },
  watch: {
    value (newVal, oldVal) {
      // console.log('新关键词:' + newVal)
      this.value = newVal
      this.getCloudSearch(this.type, this.value)
    }
  }
}
</script>

<style lang="less" scoped>
  .albuminfo {
    width: 100%;
    padding: 15px;
    display: flex;
    flex-direction: row;
    .left {
      display: flex;
      flex-direction: row;
      align-items: center;
      width: 50%;
      img {
        width: 75px;
        height: 75px;
        border-radius: 5px;
        margin-right: 15px;
      }
    }
    .artist {
      display: flex;
      align-items: center;
      span:last-child {
        margin-left: 2px;
        color: #b4b4b4;
      }
    }
  }
  .albuminfo:nth-child(2n) {
    background-color: #f9f9f9;
  }
  .albuminfo:hover {
    background-color: #f0f1f2;
  }
</style>
